<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>确认付款</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/style.css" />
		<link rel="stylesheet" href="../css/iconfont.css" />
		<link rel="stylesheet" href="../css/wallet.css" />
		<style type="text/css">
			.fuk_conbox{background: none; padding: 5% 2%;}
			.fuk_ctimg{width: 40%;}
			.ffuk_prinum{font-size: .9rem; margin: 1.8rem 0 .2rem;}
			#sale_price{font-size: 2rem; line-height: 35px; text-align: center; margin-top: 1.2rem;}
			.label{float: left; line-height: 35px;}
			.pay_spnum{ width: 100px; padding: 0 30px;}
			.mui-numbox [class*=btn-numbox]{width: 30px;}
		</style>
	</head>

	<body>
		<div class="mui-content qrfuk_content" style="background: white;">
			<div class="fuk_conbox">
				<div class="fuk_ctop">
					<div class="fuk_ctimg"><img id='store_thum'>
					<div class="fuk_spnam">龙信云支付</div>
				</div>
				<div id="sale_price">¥0.00</div>
				<div class="ffuk_prinum clearfix" style="margin-left:25%">
					<div class="label" style="color: #8c8787;">数量：</div>
					<div class="mui-numbox pay_spnum">
						<button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
						<input class="mui-input-numbox" type="number" onchange="change(this)" id="buy_number" value="1">
						<button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
					</div>
				</div>
				<button type="button" class="qrfuk_btn" id='qrfuk_btn'>确认付款</button>
			</div>
			<div class="fuk_sm" style="position: relative; margin-top: 50px;">©由龙信天下提供技术支持</div>
		</div>
		<!----------------悬浮框  设置支付密码 ------------------->
		<!----------------悬浮框  填写支付密码------------------->
		<input type="hidden" id='order_sn'>
		<input type="hidden" id='give_score'>
	</body>

</html>
<script src="../js/mui.min.js"></script>

<script type="text/javascript" src="../js/date.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/layer/layer.min.js"></script>
<script type="text/javascript" src="../js/app.js"></script>
<script>
	function change(obj){
		if($(obj).val()<=1){
			$(obj).val(1);
		}
	}
	var chain_id=getUrlParam("chain_id");//分店id
	if(!chain_id){
		chain_id="";
	}
	var store_info;
	var sale_price=0;
	$("#order_sn").val(getSn());
	$(function() {
		//记录扫码客户端，微信，或支付宝
		var pay_type = 0;
		var ua = window.navigator.userAgent.toLowerCase();
		var auth_code = getQueryString("auth_code");
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			pay_type = 2;
		} else if(ua.match(/AlipayClient/i) == 'alipayclient') {
			pay_type = 3;
			if(auth_code == null) {
				var fromurl = location.href;
				var url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016030801195057&scope=auth_base&redirect_uri='+encodeURIComponent(fromurl);
				location.href = url;
			}
		}
		
		//获取商户信息
		$.ajax({
			async: false, 
			url: APP.api_url + 'wallet/fast_goods_info',
			data: {
				store_id: getUrlParam("store_id"),
				chain_id:chain_id,
				goods_id:getUrlParam("goods_id")
			},
			success: function(data) {
				if(data.code == 200) {
					$('.fuk_spnam').text(data.goods.goods_name);
					$('#sale_price').text("￥"+data.goods.sale_price);
					sale_price=data.goods.sale_price;
					//图片地址
					var picture_url = user_tupian(data.goods.thumb,'/images/logo-ma.jpg');
					$("#store_thum").attr('src', picture_url);
					store_info=data.store;
					if(store_info['is_ads'] == 2){
						//获取用户在当前店铺鼓励金使用情况
						var member = JSON.parse(localStorage.getItem('member'));
						apply_score_user(member['id'],store_info['id'],'give_score');
					}
				} else {
					layer.msg(data.msg);
					return false;
				}
			}
		})
		
		//确认付款
		$("#qrfuk_btn").click(function() {
			$("#qrfuk_btn").attr("disabled", true);
			//付款金额
			var buy_number = $("#buy_number").val();
			if(buy_number == "") {
					layer.msg('请输入购买数量');
					$("#qrfuk_btn").attr("disabled", false);
					return false;
			}	
			var jstype = "html";
			if(pay_type == 3){
				//获取支付宝key
				alipay_user(auth_code,getUrlParam("fromuid"));
				var action="Payment/alipay";
			}else if(pay_type == 2){
				//获取openid
				var action="Payment/wechat";
				jstype = "jsonp";
			}
			var pay_money=parseFloat(buy_number)*sale_price;
			var fee =pay_money;
			//是否单店铺
			if(store_info['is_union_score'] == 1){
				//获取用户在当前店铺鼓励金使用情况
				var bili = store_info['ad_score'];
				//所需鼓励金
				var need_score = parseFloat((fee * 100 * bili) / 10000);
				var myscore=parseFloat($("#give_score").val());
				//如果鼓励金足够鼓励金抵扣鼓励金不足无法抵扣但送等额鼓励金
				if(need_score<myscore){
					need_score=myscore;
					 fee=fee-need_scoe;
					var give_score=0;
				}else{
					need_score=0;
					var give_score=fee;
					if(parseFloat(store_info['score'])<=parseFloat(give_score)){
						layer.msg("店铺鼓励金不足无法赠送鼓励金！");
						return;
					}
				}
			}else{
				//未开启鼓励金营销送0鼓励金
				var give_score=0;
				var need_score=0;
			}
			if(fee==0){
				 action='Payment/linepay_by_score';
				 pay_type=6;
			}
				$.ajax({
	             			async: false, 
							url: APP.api_url + action,
							data: {
								WIDsubject		: '商品购买',
								WIDbody			: "商品购买",
								WIDout_trade_no	:$("#order_sn").val(),
								WIDtotal_fee	: fee,
								uid				: getUid(),
								token			: get_token(),
								type			: 'line_pay',
								user_type		: 1,
								openid			: localStorage.getItem('openid'),
								pay_type		: pay_type,
								pay_money		: pay_money,
								give_score		:give_score,
								need_score		:need_score,
								store_id		: getUrlParam("store_id"),
								chain_id		:getUrlParam("chain_id"),
								goods_id		:getUrlParam("goods_id")
							},
							type: "post",
							dataType: jstype,
							jsonp: 'callback',
							jsonpCallback: 'jsonpCallback',
							success: function(data) {
								if(pay_type == 2) {
									callpay(data);
								} else {
									$("#callback").append(data);
								}
								if(pay_type == 5) {
									location.href="success_l.html?sn="+$("#order_sn").val();
								}
								
							}
						})
	               

		});
		
		//店铺链接
		$('#store_thum').click(function(){
			var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
			
			var index = "shop.html?";
			if(store_info.template_id &&  store_info.template_id!="") {
				index = "shop" + store_info.template_id + ".html?";
			}
			location.href = APP.web_root+index+'store_id='+store_info['id']+'&fromuid='+getUrlParam("store_id");
		});
	});








	
	
	
	
//只能数字和小数点
	function clearNoNum(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
	//调用微信JS api 支付
	function jsApiCall(jsApiParameters) {
		WeixinJSBridge.invoke('getBrandWCPayRequest', jsApiParameters,
		function(res) {
			if(res.err_msg == "get_brand_wcpay_request:ok") {
				layer.msg('付款成功！', {
						  time: 2000
						}, function(){
							localStorage.setItem('member', JSON.stringify(data.data));
							location.href="success_l.html?sn="+$("#order_sn").val();
						});
			} else {
				layer.msg('付款失败');
				$("#qrfuk_btn").attr("disabled",false);
			}
		});
	}

	function callpay(jsApiParameters) {
		console.log(jsApiParameters);
		if(typeof WeixinJSBridge == "undefined") {
			if(document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall(jsApiParameters), false);
			} else if(document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall(jsApiParameters));
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall(jsApiParameters));
			}
		} else {
			jsApiCall(jsApiParameters);
		}
	}
	function jsonpCallback(data) {
		$("#cut_back").append(data);
	}

	//获取用户在店铺鼓励金使用情况
	function apply_score_user(user_id,store_id,ement_id){
		$.ajax({
			//type: 'post',
			async: false, 
			url: APP.api_url + 'wallet/apply_score_user',
			data: {
				user_id		: user_id,
				store_id	: store_id,
				token 		:'cheshi'
			},
			success: function(data) {
				if(data.code == 200) {
					$("#"+ement_id).val(data.data);
				} else {
					layer.msg(data.msg);
				}
			}
		});
	}
</script>
